// categories.js
const regeneratorRuntime = require('../../lib/runtime');
const wx = require('../../lib/wx');
const Api = require('../../utils/api.js');
const wxapp = require('../../lib/wxapp');
const Util = require('../../utils/util.js');

// 获取应用实例
const app = getApp();
Page({
  data: {
    tabIndex: 0,
    carNum:0,
    categories: [],
    tabActive: '热门品牌',
    brandBox: [
      {
        logo: 'http://www.haba.com.cn/images/logo3.png',
        name: 'HABA',
        url: '../brandDetail/brandDetail',
        list:[
          {
            imgUrl: '../../images/products/product_img1.png',
            url: '../productDetail/productDetail',
            price: '189.00',
            discount: '40',
          },
          {
            imgUrl: '../../images/products/product_img2.png',
            url: '../productDetail/productDetail',
            price: '136.00',
            discount: '40',
          },
          {
            imgUrl: '../../images/products/product_img1.png',
            url: '../productDetail/productDetail',
            price: '129.00',
            discount: '40',
          },
        ],
      },
      {
        logo: 'http://www.haba.com.cn/images/logo3.png',
        name: 'THE LAUNDRESS',
        url: '../brandDetail/brandDetail',
        list:[
          {
            imgUrl: '../../images/products/product_img1.png',
            url: '../productDetail/productDetail',
            price: '189.00',
            discount: '40',
          },
          {
            imgUrl: '../../images/products/product_img2.png',
            url: '../productDetail/productDetail',
            price: '136.00',
            discount: '40',
          },
          {
            imgUrl: '../../images/products/product_img1.png',
            url: '../productDetail/productDetail',
            price: '129.00',
            discount: '40',
          },
        ],
      },
    ],
    businessBox: [
      {
        logo: 'https://www.mytheresa.com/skin/frontend/mytheresa/default/images/logo.png',
        name: 'Mytheresa',
        url: '../brandDetail/brandDetail',
        list:[
          {
            imgUrl: '../../images/products/product_img1.png',
            url: '../productDetail/productDetail',
            price: '189.00',
            discount: '40',
          },
          {
            imgUrl: '../../images/products/product_img2.png',
            url: '../productDetail/productDetail',
            price: '136.00',
            discount: '40',
          },
          {
            imgUrl: '../../images/products/product_img1.png',
            url: '../productDetail/productDetail',
            price: '129.00',
            discount: '40',
          },
        ],
      },
      {
        logo: 'https://www.reebonz.cn/img/reebonz_logo/reebonz_logo_desktop.png',
        name: 'Ssense',
        url: '../brandDetail/brandDetail',
        list:[
          {
            imgUrl: '../../images/products/product_img1.png',
            url: '../productDetail/productDetail',
            price: '189.00',
            discount: '40',
          },
          {
            imgUrl: '../../images/products/product_img2.png',
            url: '../productDetail/productDetail',
            price: '136.00',
            discount: '40',
          },
          {
            imgUrl: '../../images/products/product_img1.png',
            url: '../productDetail/productDetail',
            price: '129.00',
            discount: '40',
          },
        ],
      },
    ],
  },

  goToMenu: wxapp.handler(function* goToMenu(next, e){
    const url = e.currentTarget.dataset.url;
    wx.reLaunch({
      url,
    });

    yield next;
  }),

  goToSearch: wxapp.handler(function* goToSearch(next){
    wx.navigateTo({
      url: '../search/search',
    });

    yield next;
  }),

  switchCategoryTab: wxapp.handler(function* switchCategoryTab(next, e){
    const tabIndex = e.target.dataset.index;
    this.setData({
      tabIndex,
    });
  }),

  switchTab: wxapp.handler(function* switchTab(next, e){
    const tabActive = e.target.dataset.tab;
    this.setData({
      tabActive,
    });
  }),

  goToDetail: wxapp.handler(function* goToDetail(next, e){
    const url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    });

    yield next;
  }),

  onShow: wxapp.handler(function* onShow(next) {

    yield next;
  }),

  onLoad: wxapp.handler(function* onLoad(next) {

    yield next;

    var goodsClass = Api.getGoodsClass();
    var that = this;

    // 商品分类
    wx.request({
      url: goodsClass,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      complete: function (res) {
        console.log(res.data.data);
        that.setData({ categories: res.data.data });
      }
    });


    var getCarNum = Api.getCarNum();
    const authTokenKey = Util.authTokenKey();
    const token = wx.getStorageSync(authTokenKey);
    wx.request({
      url: getCarNum,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {//发送给后台的数据
        token: token
      },
      complete: function (res) {
        that.setData({ carNum: res.data.data });
      }
    });
  }),

  onPullDownRefresh: wxapp.handler(function* onPullDownRefresh(next) {
    yield next;

    var goodsClass = Api.getGoodsClass();
    var that = this;

    // 商品分类
    wx.request({
      url: goodsClass,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      complete: function (res) {
        console.log(res.data.data);
        that.setData({ categories: res.data.data });
      }
    });

    wx.stopPullDownRefresh();
  }),

});
